<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="displayModuleTitle"
    @ok="onOkButtonClick"
  >
    <BasicForm @register="registerForm">
      <template #sign="{ model, field }">
        <div style="margin-bottom: 5px"
          ><a-button type="primary" @click="onOpenEsignModalButtonClick">打开签名板</a-button>
          <a-button danger type="text" v-if="model[field]" @click="setSignValue(null)"
            >清空</a-button
          ></div
        >
        <a-image :src="getImageFileUrlPrefix + model[field]" :width="150" v-if="model[field]" />
      </template>
    </BasicForm>
    <EsignModal @register="registerSignModal" @success="setSignValue" />
    <Loading :loading="loading" tip="加载中" />
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, onMounted, ref, unref } from 'vue';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { troubleCheckFormSchema } from './troubleCorrect.data';
  import { useGlobSetting } from '/@/hooks/setting';
  import { getImageFileApi } from '/@/api/platform/file';
  import { useGlobSetting } from '/@/hooks/setting';
  import { checkEhsTroubleCorrect, getOneEhsTroubleCorrectInfo } from '/@/api/ehs/troublecorrect';
  import EsignModal from '../Component/EsignModal.vue';
  import { Loading } from '/@/components/Loading';
  import { useUserStore } from '/@/store/modules/user';
  const { globalProjectCode } = useGlobSetting();
  const { createMessage } = useMessage();

  export default defineComponent({
    components: { BasicModal, BasicForm, EsignModal, Loading },
    name: 'TroubleCheckModal',
    emits: ['success'],
    setup(props, { emit }) {
      const loading = ref(false);
      const userStore = useUserStore();
      const { uploadUrl } = useGlobSetting();
      const getImageFileUrlPrefix = ref<string>(uploadUrl + getImageFileApi);
      const [registerSignModal, { openModal: openEsignModal }] = useModal();
      const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
        labelWidth: 100,
        schemas: troubleCheckFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 24,
        },
      });
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const dataId = ref();
      const [register, { closeModal }] = useModalInner((data) => {
        resetFields();
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.dataId) {
            dataId.value = unrefData.dataId;
            getOneEhsTroubleCorrectInfo(dataId.value).then((res) => {
              setFieldsValue({
                endtm: res.checktm,
                username: res.checkusername,
                hascheck: res.hascheck,
                description: res.checkdescription,
                attachment: res.checkattachment ? JSON.parse(res.checkattachment) : [],
                sign: res.checksign ? res.checksign : userStore.getUserInfo?.sign,
              });
            });
          }
        }
      });

      function onOpenEsignModalButtonClick() {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '隐患治理整改',
          operateString: '签字',
          dataId: dataId.value,
        };
        openEsignModal(true, propsData);
      }

      function setSignValue(sign) {
        setFieldsValue({
          sign: sign,
        });
      }

      async function onOkButtonClick() {
        const values = await validate();
        loading.value = true;
        values.id = dataId.value;
        values.attachment = JSON.stringify(values.attachment);
        checkEhsTroubleCorrect(values)
          .then((res) => {
            if (!res) {
              createMessage.success('验收成功');
              emit('success');
              closeModal();
            } else createMessage.error('整改失败,请稍后重试');
            loading.value = false;
          })
          .catch((e) => {
            loading.value = false;
            console.log(e);
          });
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });

      onMounted(() => {});

      return {
        register,
        loading,
        getImageFileUrlPrefix,
        registerSignModal,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
        setSignValue,
        onOpenEsignModalButtonClick,
      };
    },
  });
</script>
